<?php

// phpMyRealty 3
//
// File Name: viewuser.php
// File Location : ./
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// Include configuration file and general functions
define('PMR', 'true');

include ( './config.php' );
include ( PATH . '/defaults.php' );

// ----------------------------------------------------------------------
// View User SECTION

// Check the id variable
if (isset($_GET['id']) && eregi('^[0-9]+$', $_GET['id']))

 {

  $sql = 'SELECT first_name, last_name, description, company_name, address, city, zip, phone, location FROM ' . USERS_TABLE  . ' WHERE approved = 1 AND id = "' . safehtml($_GET['id']) . '" LIMIT 1';
  $r_title = $db->query ( $sql );
  if ($db->numrows($r_title) < 1) { echo 'Listing Does Not Exist or Was Not Approved Yet.'; exit(); }
  $f_title = $db->fetcharray ( $r_title );

 }

else

 {
  echo 'Listing Does Not Exist.'; exit(); 
 }

// Title tag content
$title = $conf['website_name_short'] . ' - ' . $f_title['first_name'] . ' ' . $f_title['last_name'];

$meta_title = trim(removehtml(unsafehtml_xml($f_title['first_name']) . ' ' . $f_title['last_name']));
$meta_description = removehtml(unsafehtml_xml($f_title['description']));
$meta_description = trim(substr($meta_description, 0, 160));
$meta_keywords = trim(removehtml(unsafehtml_xml($f_title['first_name']) . ', ' . $f_title['last_name'] . ', ' . $f_title['company_name'] . ', ' . $f_title['address'] . ', ' . $f_title['city'] . ', ' . $f_title['zip'] . ', ' . getnamebyid(LOCATIONS_TABLE, $f_title['location']) . ', ' . $f_title['phone']));

// Template header
include ( PATH . '/templates/' . $cookie_template . '/header.php' );

// Check the id variable
if (isset($_GET['id']) && eregi('^[0-9]+$', $_GET['id']))

 {
  // Rating system
  // - a routine to process the submitted vote
  if (isset($_GET['req']) && $_GET['req'] == 'rating')
   {
    echo table_header ( $lang['Information'] );
    // If something <0 or >5 was submitted we process it as 5
    if ($_POST['vote'] > 5 || $_POST['vote'] < 0) $vote = 5; else $vote = $_POST['vote'];

    $id = safehtml($_GET['id']);

    // Get the user IP address
    $user_ip = $_SERVER['REMOTE_ADDR'];
    // If there is more than one IP
    // get the first one from the 
    // comma separated list
    if ( strstr($user_ip, ', ') ) 
     {
      $ips = explode(', ', $user_ip);
      $user_ip = $ips[0];
     }

    // Check if this user have already voted for this user
    $sql = 'SELECT id FROM ' . RATINGS_TABLE . ' WHERE ip = "' . $user_ip . '" AND userid = "' . $id . '"';
    $r = $db->query($sql) or error ('Critical Error', mysql_error());
    if ($db->numrows($r) == 0)
     {

      // Updating the ratings table
      $sql = 'INSERT INTO ' . RATINGS_TABLE . ' (ip, userid, rating, date) 
              VALUES ("' . $user_ip . '", "' . $id . '", "' . $vote . '", "' . date ('Y-m-d') . '")';

      $db->query($sql) or error ('Critical Error', mysql_error());

      // Updating the users table
      $sql = 'UPDATE ' . USERS_TABLE . ' SET rating = rating + ' . $vote . ' , votes = votes + 1 WHERE id = ' . $id;

      $db->query($sql) or error ('Critical Error', mysql_error());

      echo $lang['Realtor_Votes_Submitted'];

     }

    else 
     // Print error message if this user votes for the second time
     // for one and the same user
     echo $lang['Realtor_Votes_Repeat'];

    echo table_footer ();
   }

  // Navigation
  echo table_header ( $lang['Navigation'] );
  // Home page link
  echo '- <a href="' . URL . '/">' . $lang['Menu_Home'] . '</a><br />';

  $usersearch = $session->fetch('usersearchvariables');
  $listingsearch = $session->fetch('listingsearchvariables');
  // If the user search cache is not empty we print the
  // link to the latest search results
  if (!empty($usersearch))
   echo '- <a href="' . URL . '/usersearch.php?' . $session->fetch('usersearchvariables') . '">' . $lang['Search_Back_User'] . '</a><br />';

  // If the listings search cache is not empty we print the
  // link to the latest search results
  if (!empty($listingsearch))
   echo '- <a href="' . URL . '/search.php?' . $session->fetch('listingsearchvariables') . '">' . $lang['Search_Back_Listing'] . '</a><br />';

  echo table_footer ();

  //

  echo table_header ( $lang['Information'] );

  // Update HITS counter

  $sql = 'UPDATE ' . USERS_TABLE  . ' SET hits = hits+1 WHERE approved = 1 AND id = "' . safehtml($_GET['id']) . '" LIMIT 1';
  // We process the visitor counter update only if 24 hours 
  // passed since the last visit or this user have visited 
  // a different listing before getting back to this one
  if ( $session->fetch('user_counter_id') != $_GET['id'] 
  || (time() - $session->fetch('user_counter_time')) > 60*60*24 )
   {
    $db->query ( $sql ) or error ('Critical Error' , mysql_error());
   }

  $session->set('user_counter_id', intval($_GET['id']));
  $session->set('user_counter_time', time());

  //

  $sql = 'SELECT * FROM ' . USERS_TABLE  . ' WHERE approved = 1 AND id = "' . safehtml($_GET['id']) . '" LIMIT 1';
  $r = $db->query ( $sql );

  echo '	
   <table width="100%" cellpadding="5" cellspacing="0" border="0">
       ';

  $f = $db->fetcharray( $r );

  if ($f['package'] != '0' && $f['package'] != '')
   {

    $sql = 'SELECT * FROM ' . PACKAGES_AGENT_TABLE  . ' WHERE id = ' . $f['package'] . ' LIMIT 1';
    $r_package = $db->query ( $sql );
    $f_package = $db->fetcharray ( $r_package );

   }

  else

   {

    $f_package['listings'] = $conf['free_listings'];
    $f_package['gallery'] = $conf['free_gallery'];
    $f_package['mainimage'] = $conf['free_mainimage'];
    $f_package['photo'] = $conf['free_photo'];
    $f_package['phone'] = $conf['free_phone'];
    $f_package['address'] = $conf['free_address'];

   }

  // Starting a new template
  $template = new Template;

  $tpl = implode ('', file( PATH . '/templates/' . $cookie_template . '/tpl/realtor_detailed.tpl' ));

  // Load user detailed template
  $template->load ( $tpl );

  // Change empty values in the array to 'n/a'
  $f = array_map ( 'if_empty', $f);

  // Replace the template variables
  if ($f_package['photo'] == 'ON')
   $template->set ( 'photo', show_gallery_image ('photos', $f['id']) );
  else
   $template->set ( 'photo', '' );

  $template->set ( 'first_name', $f['first_name'] );
  $template->set ( 'last_name', $f['last_name'] );
  $template->set ( 'company_name', $f['company_name'] );
  $template->set ( 'description', nl2br(html_entity_decode($f['description'])) );
  $template->set ( 'location', getnamebyid ( LOCATIONS_TABLE, $f['location'] ) );

  if ($f_package['address'] == 'ON')
   {
    $template->set ( 'address', $f['address'] );
    $template->set ( 'city', $f['city'] );
    $template->set ( 'zip', $f['zip'] );
   }
  else
   {
    $template->set ( 'address', '' );
    $template->set ( 'city', '' );
    $template->set ( 'zip', '' );
   } 

  if ($f_package['phone'] == 'ON')
   {
    $template->set ( 'phone', $f['phone'] );
    $template->set ( 'fax', $f['fax'] );
    $template->set ( 'mobile', $f['mobile'] );
   }
  else
   {
    $template->set ( 'phone', '' );
    $template->set ( 'fax', '' );
    $template->set ( 'mobile', '' );
   }

  $template->set ( 'email', validateemail ( $f['id'], $f['email'] ) . $lang['Realtor_Send_Message'] . '</a>' );
  $template->set ( 'website', validatewebsite ( $f['id'], $f['website'] ) . $lang['Realtor_Visit_Website'] . '</a>' );

  // Defaults for their account type
  $paid_account = false;
  $account = false;
  $agent_details = '';
  
  	// If they have an account
  	if (auth_check($session->fetch('login'), $session->fetch('password')))
  	{
		  // Fetching the user ID from the user's table
		  $sql = 'SELECT approved, id, package FROM ' . USERS_TABLE . ' WHERE login = "' . $session->fetch('login') . '" LIMIT 1';
		  $r = $db->query( $sql );
		  $f1 = $db->fetcharray( $r );
		  
		  $account = true;
		  
		  if ($f1['package'] > 0)
		  	$paid_account = true;
		  else
		  	$paid_account = false;
  	}
  	else
  	{
  		$paid_account = false;
  		$account = false;
  	}
  
	// Agent details (whether this displays is configurable via admin panel > configuration settings)
	
	// If anyone can view agent details or registered only but they are logged in or paid only but they are paid
	if ($conf['contact_agents'] == '2' && $account == false)
	{
		$agent_details = $lang['Agent_Details_Account_Only'];
		$template->set ( 'view_user_listings', $agent_details );
	}
	// Paid account only, but they don't have a paid account
	elseif ($conf['contact_agents'] == '3' && $paid_account == false)
	{
		$agent_details = $lang['Agent_Details_Pay_Only'];
		$template->set ( 'view_user_listings', $agent_details );
	}
		else
	{
		$template->set ( 'view_user_listings', viewuserlistings ( $f['id'] )  );
	}

  $template->set ( 'date_added', printdate($f['date_added']) );
  $template->set ( 'date_updated', printdate($f['date_updated']) );
  $template->set ( 'ip_added', $f['ip_added'] );
  $template->set ( 'ip_updated', $f['ip_updated'] );

  $template->set ( 'hits', $f['hits'] );

  $template->set ( 'new', newitem ( USERS_TABLE, $f['id'], $conf['new_days']) );
  $template->set ( 'updated', updateditem ( USERS_TABLE, $f['id'], $conf['updated_days']) );
  $template->set ( 'top', topitem ( $f['rating'], $f['votes'] ) );

  $voting_form = '<form action="' . URL . '/viewuser.php?req=rating&amp;id=' . $_GET['id'] . '" method="POST"> <select name="vote" onChange="this.form.submit()">   <option value=""> ' . $lang['Rate_This_Listing'] . ' </option>   <option value="5">' . $lang['Realtor_Vote_5'] . '</option>    <option value="4">' . $lang['Realtor_Vote_4'] . '</option>    <option value="3">' . $lang['Realtor_Vote_3'] . '</option>    <option value="2">' . $lang['Realtor_Vote_2'] . '</option>    <option value="1">' . $lang['Realtor_Vote_1'] . '</option> </select> </form>';

  $template->set ( 'voting_form', $voting_form );

  $template->set ( 'rating', rating ( $f['rating'], $f['votes'] ) );

  // Names

  $template->set ( '@first_name', $lang['Realtor_First_Name'] );
  $template->set ( '@last_name', $lang['Realtor_Last_Name'] );
  $template->set ( '@company_name', $lang['Realtor_Company_Name'] );
  $template->set ( '@description', $lang['Realtor_Description'] );
  $template->set ( '@location', $lang['Location'] );
  $template->set ( '@city', $lang['City'] );
  $template->set ( '@address', $lang['Realtor_Address'] );
  $template->set ( '@zip', $lang['Zip_Code'] );
  $template->set ( '@phone', $lang['Realtor_Phone'] );
  $template->set ( '@fax', $lang['Realtor_Fax'] );
  $template->set ( '@mobile', $lang['Realtor_Mobile'] );
  $template->set ( '@email', $lang['Realtor_e_mail'] );
  $template->set ( '@website', $lang['Realtor_Website'] );
  $template->set ( '@date_added', $lang['Date_Added'] );
  $template->set ( '@date_updated', $lang['Date_Updated'] );
  $template->set ( '@hits', $lang['Hits'] );

  $template->set ( '@image_url', URL . '/templates/' . $cookie_template . '/images' );

  // Publish template
  $template->publish();

  echo '	
   </table>
       ';

  echo table_footer ();

 }

else

 echo 'Sorry, ID is not specified';

// Template footer
include ( PATH . '/templates/' . $cookie_template . '/footer.php' );

?>